const shader = require('../shader/index');
const parabolaLine = require('../util/parabolaLine');

let FSIZE = 0;

module.exports = class DrawLine {
    // option: {
    //     id: 'starBackground',
    //     gl: gl,
    //     globe: this,
    //     type: 'texture',
    //     radius: this.radius,
    //     isTiles: false,
    //     isTileRender: false,
    //     textureUrl: 'static/images/base.png'
    // }
    constructor(option) {
        // init config

        // init shader
        this.gl = option.gl;
        const gl = this.gl;
        const program = gl.createProgram();
        this.program = program;
        const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
        gl.shaderSource(fragmentShader, shader.drawLine.fragmentSource);
        gl.compileShader(fragmentShader);
        gl.attachShader(program, fragmentShader);
        const vertexShader = gl.createShader(gl.VERTEX_SHADER);
        gl.shaderSource(vertexShader, shader.drawLine.vertexSource);
        gl.compileShader(vertexShader);
        gl.attachShader(program, vertexShader);
        gl.linkProgram(program);

        // get attribLocation
        this.attribLocation = {
            a_Position: gl.getAttribLocation(program, 'a_Position')
        }

        this.uniformLocation = {
            u_ModelMatrix: gl.getUniformLocation(program, 'u_ModelMatrix'),
            u_ViewMatrix: gl.getUniformLocation(program, 'u_ViewMatrix'),
            u_ProjMatrix: gl.getUniformLocation(program, 'u_ProjMatrix'),
        }

        // const vertices = new Float32Array([
        //     -2.0, 0.0, 0.0, 
        //     2.0, 0.0, 0.0, 
        //     0.0, -2.0, 0.0,
        //     0.0, 2.0, 0.0
        // ]);
        // const n = 4; // The number of vertices

        const vertices = parabolaLine([116.3, 39.9], [139.75, 35.68], 50, 0.5, 2.011);
        const n = vertices.length / 3;

        var vertexBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);　 // Write date into the buffer object
        gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);

        FSIZE = vertices.BYTES_PER_ELEMENT;

        this.buffer = {
            vertexBuffer: vertexBuffer,
            n: n
        }

    }

    render(config) {
        const modelMatrix = config.modelMatrix;
        const viewMatrix = config.viewMatrix;
        const projMatrix = config.projMatrix;
        const normalMatrix = config.normalMatrix;

        const gl = this.gl;
        gl.useProgram(this.program);
        // gl.disable(gl.DEPTH_TEST);

        gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer.vertexBuffer);　 // Write date into the buffer object
        // Assign the buffer object to a_Position variable
        gl.vertexAttribPointer(this.attribLocation.a_Position, 3, gl.FLOAT, false, 0, 0);

        // Enable the assignment to a_Position variable
        gl.enableVertexAttribArray(this.attribLocation.a_Position);

        gl.uniformMatrix4fv(this.uniformLocation.u_ModelMatrix, false, modelMatrix);
        gl.uniformMatrix4fv(this.uniformLocation.u_ViewMatrix, false, viewMatrix);
        gl.uniformMatrix4fv(this.uniformLocation.u_ProjMatrix, false, projMatrix);

        gl.drawArrays(gl.LINE_STRIP, 0, this.buffer.n);
        // gl.enable(gl.DEPTH_TEST);

    }
}